package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermissions;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVO;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Stream;

@RestController
@RequestMapping("/travels")
public class TravelController {
   @Autowired
    private IRegionService regionService;
   @Autowired
   private IUserInfoService userInfoService;
   @Autowired
   private ITravelService travelService;
   @Autowired
   private IDestinationService destinationService;
   @Autowired
   private ITravelCommentService travelCommentService;
   @Autowired
   private ITravelStatisVORedisService travelStatisVORedisService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page<Travel> page = travelService.query(qo);
        List<Travel> content = page.getContent();
        for (Travel travel : content) {
            travel.setAuthor(userInfoService.selectOne(travel.getUserId()));
        }
        return JsonResult.success(page);
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.findById(id);
        travel.setAuthor(userInfoService.selectOne(travel.getUserId()));
        travel.setDest(destinationService.selectOne(travel.getDestId()));
        travelStatisVORedisService.replynumIncrease(id,1 );
        return JsonResult.success(travel);
    }


    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo){
        travel.setUserId(userInfo.getId());
        Travel travel1 = travelService.saveOrUpdate(travel);
        return JsonResult.success(travel1.getId());
    }

    /*@GetMapping("/token")
    public Object token(@UserParam UserInfo userInfo){
        boolean result = false;
       if (userInfo != null){
           result = true;
       }
        return JsonResult.success(result);
    }*/

    @GetMapping("/tv")
    public Object tv(String id){
        return JsonResult.success(travelService.findById(id));
    }

   /* @GetMapping("/UserEstimateAuthor")
    public Object UserEstimateAuthor(String id,@UserParam UserInfo userInfo){
       boolean result = travelService.UserEstimateAuthor(id,userInfo);
        return JsonResult.success(result);
    }*/


    /**
     * 增加评论
     * @return
     */
    @LoginPermissions
    @PostMapping("/commentAdd")
    public Object addComment(TravelComment travelComment, @UserParam UserInfo userInfo){
        travelCommentService.addComment(travelComment, userInfo);
        return JsonResult.success();
    }

    /**
     * 评论分页
     * @return
     */
    @GetMapping("/comments")
    public Object comments(TravelCommentQuery qo){
        List<TravelComment> travelComments = travelCommentService.findByTravelId(qo.getTravelId());
        return JsonResult.success(travelComments);
    }

    @GetMapping("/travelVo")
    public Object statisVo(String tid){
        TravelStatisVO travelStatisVO = travelStatisVORedisService.statisVo(tid);
        return JsonResult.success(travelStatisVO);
    }

    /**
     * 收藏操作
     * @param tid
     * @return
     */
    @LoginPermissions
    @PostMapping("/favor")
    public Object favor(String tid ,@UserParam UserInfo userInfo){

        boolean ret = travelStatisVORedisService.favor(tid, userInfo.getId());
        return JsonResult.success(ret);
    }

    @LoginPermissions
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String tid, @UserParam UserInfo userInfo){
        boolean ret = travelStatisVORedisService.travelThumbup(tid, userInfo.getId());

        return JsonResult.success(ret);
    }
}
